<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_form_validation extends CI_Form_validation {	
    public function valid_phone($value)
	{
		$value = trim($value);  // Clears white space
		$count = strlen(preg_replace('/\D/', '', $value)); // Counts number of digits		
		if ($value == '')
		{
			return TRUE;
		}		
		if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $value) && $count == 10)
		{
			return preg_replace('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', '($1) $2-$3', $value);
		}		
		$this->set_message('valid_phone', 'Invalid phone number');		
		return FALSE;
	}
	public function check_password($value)
	{
		$userID = $this->CI->input->post('user_id');
		$password = crypt(trim($value), $this->CI->config->item('encryption_key'));
		$query = $this->CI->db->get_where('user', array('user_id' => $userID, 'password' => $password));
		if ($query->num_rows() == 1)
		{
			return TRUE;
		}
		$this->set_message('check_password', 'That is not your current password');
		return FALSE;
	}
}